<template>
  <el-dialog
    top="30px"
    width="800px"
    title="登记追偿"
    append-to-body
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <p>选择代偿：</p>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <recovery-edit
      :visible.sync="recoveryVisible"
      :query-id="queryId"
      :comps-id="compensatoryRow.id"
      :table-id="recoveryId"
      :form-data="recoveryRow"
      @success="handleSuccess"
    ></recovery-edit>
  </el-dialog>
</template>

<script>
import RecoveryEdit from '@/credit/views/city/risk/recovery/RecoveryEdit'

export default {
  components: {
    RecoveryEdit,
  },
  props: {
    visible: Boolean,
    queryId: String,
  },
  data() {
    return {
      order: {
        create_time: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        'customer_related_id.eq': '',
      },
      loading: false,
      tableName: 'comps_view',
      tableData: [],
      tableColumns: [
        {
          attrs: { label: '代偿时间', prop: 'reach_time', align: 'center', width: '100px' },
          process: v => this.$utils.dateFormat(v, 'yyyy-MM-dd'),
        },
        { attrs: { label: '代偿合计金额（元）', prop: 'reach_total_amount', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: 130 },
          operations: [
            {
              name: '登记追偿',
              event: this.handleEdit,
            },
          ],
        },
      ],
      totalCount: 0,

      // 登记追偿
      recoveryVisible: false,
      recoveryId: '',
      recoveryRow: {},
      compensatoryRow: {},
    }
  },
  computed: {},
  watch: {
    visible(val) {
      if (val) {
        this.getData()
      }
    },
  },
  created() {},
  mounted() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    getData(index = 1) {
      const { queryId } = this
      if (!queryId) return

      this.params['customer_related_id.eq'] = queryId
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = data.data || []
          this.totalCount = data.totalCount || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleEdit(row) {
      this.compensatoryRow = { ...row }
      this.recoveryVisible = true
    },
    handleSuccess() {
      this.beforeClose()
      this.$emit('success')
    },
  },
}
</script>

<style lang="scss" scoped></style>
